package com.ruoyi.web.controller.commodity;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.commodity.service.IContractTemplatesService;
import com.ruoyi.commodity.service.IPaymentService;
import com.ruoyi.commodity.service.IproductCatesService;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.commodity.service.IVendorService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/commodity/vendor")
public class VendorController extends BaseController
{
    @Autowired
    private IVendorService vendorService;

    @Autowired
    private IproductCatesService productCatesService;

    @Autowired
    private IPaymentService paymentService;

    @Autowired
    private IContractTemplatesService contractTemplatesService;
    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/list")
    public TableDataInfo list(Vendor vendor)
    {
        startPage();
        List<Vendor> list = vendorService.selectVendorList(vendor);
        return getDataTable(list);
    }

    /**
     * 新增产品处查询供应商管理列表
     */
    @GetMapping("/product/list")
    public TableDataInfo listProductVendor(Vendor vendor)
    {
        List<Vendor> list = vendorService.selectVendorList(vendor);
        return getDataTable(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vendor vendor)
    {
        List<Vendor> list = vendorService.selectVendorList(vendor);
        ExcelUtil<Vendor> util = new ExcelUtil<Vendor>(Vendor.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vendorService.selectVendorById(id));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Vendor vendor)
    {
        return toAjax(vendorService.insertVendor(vendor));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vendor vendor)
    {
        return toAjax(vendorService.updateVendor(vendor));
    }

    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vendorService.deleteVendorByIds(ids));
    }

    /**
     * 查询产品分类
     * @param productCates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/vendorTree")
    public AjaxResult vendorTree(ProductCates productCates){
        return success(productCatesService.selectProductTreeList(productCates));

    }

    /**
     * 查询支付方式分类
     * @param payment
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/paymentTree")
    public AjaxResult paymentTree(PaymentMethod payment){
        return success(paymentService.selectPaymentTreeList(payment));

    }

    /**
     * 生成供应商编号
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/vendorNumber")
    public AjaxResult vendorNumber(){
        return success(vendorService.selectNumber());
    }

    /**
     * 查询合同模板分类
     * @param contractTemplates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/templatesTree")
    public AjaxResult templatesTree(ContractTemplates contractTemplates){
        return success(contractTemplatesService.selectTemplatesTreeList(contractTemplates));

    }

    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")
    @GetMapping("/selectTempltesById/{id}")
    public AjaxResult selectTempltesById(@PathVariable Long id){
        return success(contractTemplatesService.selectContractTemplatesById(id));
    }

    @PreAuthorize("@ss.hasPermi('commodity:vendor:edit')")
    @PostMapping("/batchUpdateContract")
    public AjaxResult batchUpdateContract(@RequestBody List<Vendor> vendorList){
        return success(vendorService.batchUpdate(vendorList));
    }

    @PreAuthorize("@ss.hasPermi('commodity:vendor:list')")

    @GetMapping("/selectVendorList/{ids}")
    public AjaxResult selectVendorList(@PathVariable Long[] ids){
        return success(vendorService.selectVendorListById(ids));
    }

    /**
     * 查询合同
     * @param contractTemplates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:templatesList')")
    @GetMapping("/selectContractTemplatesList")
    public TableDataInfo selectContractTemplatesList(ContractTemplates contractTemplates){
        startPage();
        List<ContractTemplates> list = contractTemplatesService.selectContractTemplatesList(contractTemplates);
        return getDataTable(list);
    }

    /**
     * 添加合同
     * @param contractTemplates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:templatesAdd')")
    @PostMapping("/insertTemplates")
    public AjaxResult insertTemplates(@RequestBody ContractTemplates contractTemplates){
        contractTemplates.setAuthor(getNickname());
        contractTemplates.setAddTime(DateUtils.getNowDate());
        return toAjax(contractTemplatesService.insertContractTemplates(contractTemplates));
    }

    /**
     * 合同详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:templatesUpdate')")
    @GetMapping("/selectContractTemplatesById/{id}")
    public AjaxResult selectContractTemplatesById(@PathVariable Long id){
        return success(contractTemplatesService.selectContractTemplatesById(id));
    }

    /**
     * 修改合同
     * @param contractTemplates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:templatesUpdate')")
    @PutMapping("/updateContractTemplates")
    public AjaxResult updateContractTemplates(@RequestBody ContractTemplates contractTemplates){
        return toAjax(contractTemplatesService.updateContractTemplates(contractTemplates));
    }

    @PreAuthorize("@ss.hasPermi('commodity:vendor:templatesDelete')")
    @DeleteMapping("/deleteByIdInt/{ids}")
    public AjaxResult deleteByIdInt(@PathVariable Long[] ids){
        return toAjax(contractTemplatesService.deleteByIdInt(ids));
    }

    /**
     * 导入供应商模版数据
     * @param file
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:vendor:importVendor')")
    @PostMapping("/importVendorExcel")
    public String importVendorExcel(@RequestParam("file") MultipartFile file) {
        int row = vendorService.importVendorExcel(file);
        return "Excel 导入成功，新增供应商：" +row + " 条";
    }
}
